Enhance save_state method with warnings handling#444
Open
tentpegbob wants to merge 1 commit into
Open
Conversation
`ReplState.save_state()` hangs indefinitely when the REPL namespace contains an enum (or other type with recursive `__main__` self-references). The `dill.dumps()` picklability test enters exponential recursion, emitting thousands of `PicklingWarning`s before `RecursionError` eventually propagates to the `except BaseException` handler. **Impact:** Any agent using `python_repl` that executes code defining an enum class will hang on the next `save_state()` call, blocking the entire agent process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
ReplState.save_state()hangs indefinitely when the REPL namespace contains an enum (or other type with recursive__main__self-references). Thedill.dumps()picklability test enters exponential recursion, emitting thousands ofPicklingWarnings beforeRecursionErroreventually propagates to theexcept BaseExceptionhandler.Impact: Any agent using
python_replthat executes code defining an enum class will hang on the nextsave_state()call, blocking the entire agent process.When
dill.dumps()encounters an enum defined in__main__, it tries to pickle by reference. The qualified name__main__.Constantsresolves back to the namespace containingConstants, which itself has members that reference the class — creating infinite recursion. BeforeRecursionErrorpropagates, dill emits aPicklingWarningat each recursion level. With Python's default recursion limit of 1000, this creates an exponential blowup of warnings and CPU time that effectively hangs the process.The existing
except BaseException: continueon line 223 is correct but never reached in time — dill spends minutes in recursive attempts before the exception fires.Related Issues
N/A
Documentation PR
N/A
Type of Change
Bug fix
Testing
Two targeted changes in
save_state():Tight recursion limit during
dill.dumps()test —sys.setrecursionlimit(200)before each picklability test, restored infinally. Self-referencing objects hitRecursionErrorin milliseconds instead of minutes.Suppress dill
PicklingWarning—warnings.catch_warnings()context manager filters dill warnings during both the per-object test and the finaldill.dump(). These warnings are noise (theexcepthandler already skips unpicklable objects).Reproduction
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.